package algothrim

func isHappy(n int) bool {
	slow, fast := n, getNext(n)
	for fast != 1 && slow != fast {
		slow = getNext(slow)
		fast = getNext(getNext(fast))
	}
	return fast == 1
}

func getNext(n int) int {
	totalSum := 0
	for n > 0 {
		digit := n % 10
		totalSum += digit * digit
		n /= 10
	}
	return totalSum
}

func isHappyI(n int) bool {
	slow, fast := n, getNextI(n)
	for fast != 1 && slow != fast {
		slow = getNext(slow)
		fast = getNext(getNext(fast))
	}
	return fast == 1
}

func getNextI(n int) int {
	totalSum := 0
	for n > 0 {
		digit := n % 10
		totalSum += digit * digit
		n = n / 10
	}
	return totalSum
}
